package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.repository.StrategyCommentMongoRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    
    @Autowired
    private StrategyCommentMongoRepository strategyCommentMongoRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private StrategyServiceImpl strategyService;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        strategyCommentMongoRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentMongoRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentMongoRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentMongoRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentMongoRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {
        // 创建查询对象
        Query query = new Query();

        // 查询总条数
        long count = template.count(query, StrategyComment.class);
        // 给评论分页添加条件
        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        // 调用PageRequest 方法进行分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");
        // 给查询对象设置条件
        query.with(pageable);
        // 从mongodb 中查询符合条件的数据
        List<StrategyComment> strategyComments = template.find(query, StrategyComment.class);
        // 返回当前页数据,分页和总长度
        return new PageImpl<>(strategyComments , pageable , count);
    }

    @Override
    public void commentThumb(String cid, String id) {
        // 通过评论id 获取当前评论对象
        StrategyComment strategyComment = this.get(cid);
        // 通过当前评论对象获取当前用户点赞集合
        List<String> thumbuplist = strategyComment.getThumbuplist();
        if(thumbuplist.contains(id)){
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(id);
        }else {
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(id);
        }
        this.update(strategyComment);
    }



}
